<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel common\models\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <div style="text-align:center">
<!--        <button type="button" style="height: 25px;width: 50px" onclick="checkClick()">确定-->
            <button type="button" style="float: right;margin-bottom: 20px" class="btn btn-primary" onclick="checkClick()">文章推送</button>
    </div>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
           // ['class' => 'yii\grid\SerialColumn'],

            'id',
            [
                'label' => '系统名称',
                'attribute' => 'username',
                'format' => 'raw',
            ],
            [
                'label' => '微信昵称',
                'attribute' => 'wxname',
                'format' => 'raw',
            ],
            [
                'label' => '头像',
                'attribute' => 'icon',
                'format' => 'raw',
                //通过该返回值，我们可以任意控制列数据的显示
                //$data指向的是当前行的数据结果集
                'value' => function ($data) {
                    return '<img src="'  . ltrim($data->icon, '/') . '" width="50px">';
                },
            ],
            [
                'label' => '性别',
                //设置筛选选项
                //'filter' => [1 => '男', 2 => '女',],
                'attribute' => 'sex',
                'format' => 'raw',
                'value' => function ($data) {
        $u = '';
        if ($data->sex == 1){
            $u ='男';
        }if ($data->sex == 2){
                        $u ='女';
                    }

                    return $u;
                }
            ],
            [
                'label' => '城市',
                'attribute' => 'city',
                'format' => 'raw',
            ],
            [
                'label' => '加入时间',
                'attribute'=>'created_at',
                'format'=>['date','php:Y-m-d H:i:s'],
            ],
            [
                'label' => '状态',
                //设置筛选选项
                'filter' => [0 => '正常', 1 => '封禁'],
                'attribute' => 'status',
                'format' => 'raw',
                'value' => function ($data) {
                    return ($data->status == 0) ? '正常' : '封禁';
                }
            ],
            [
                'header' => '操作',
                'class' => 'yii\grid\ActionColumn',
                //设置显示模板
                'template' => '{upd} {del}',
                //下面的按钮设置，与上面的模板设置相关联
                'buttons' => [
                    'upd' => function ($url, $model, $key) {
                       $url = \yii\helpers\Url::toRoute(['user/forbidden', 'id' => $key]) ;
                        $options=[
                            'title'=>Yii::t('yii', '封禁'),
                            'aria-label'=>Yii::t('yii','封禁'),
                            'data-confirm'=>Yii::t('yii','你确定要封禁/解封该用户吗？'),
                            'data-method'=>'post',
                            'data-pjax'=>'0',
                        ];
                        return Html::a('<span class="	glyphicon glyphicon-remove"></span>',$url,$options);


                        // return '<a href="' . \yii\helpers\Url::toRoute(['test/upd', 'id' => $key]) . '" class="btn btn-warning">修改</a>';
                    },
//                    'del' => function ($url, $model, $key) {
//                        return '<a href="' . \yii\helpers\Url::toRoute(['test/del', 'id' => $key]) . '" class="btn btn-danger">删除</a>';
//                    },
                ],
            ],



        ],
    ]); ?>
</div>
<script>
    function checkClick() {
        if(window.confirm('你确定推送消息吗？')){
            token = "<?php echo \Yii::$app->request->getCsrfToken()?>",
                $.ajax({

                    url: "index.php?r=user/upd",
                    data: {_csrf: token},
                    type: "post",
                    dataType : 'json',
                    success: function (msg) {
                        if (msg.status ==200){
                            alert(msg.msg)
                        }else {
                            alert(msg.msg)
                        }

                    }


                });
        }
        else {
            return false;
        }


    }
</script>
